// pages/02admin/00waiter/waiter.js

var co = require('../../../resource/js/common.js')
var app = getApp()
var _this, _data, _qrParam, _opt

Page({
   data: {
      worder: '',
      workerList: [], //工作人员列表
      i: '',
      printerList: [],
      mode: '',
      edit_id: -1,
      inviteQRCode: '' //二维码图片
   },

   //进入页面执行
   onLoad: function(options) {
      _this = this
      _data = this.data
      // options = {
      //    User_Identity:'boss',
      //    biz_ID:'580'
      // }
      _opt = options
      console.log('o==', options)
      console.log('app.Data', app.Data)
      this.getWorkerList()
      this.createQRCode()
   },
   //获取员工列表 
   getWorkerList: function() {
      co.getShareApi('jyhs/Usermg/getWorkerList', {
         Biz_ID: _opt.biz_ID
      }, function(res) {
         console.log('res', res)
         if (res.code == 0) {
            var list = _data.workerList.concat(res.data)
            _this.setData({
               workerList: list
            })
         }
      })
   },

   //修改员工信息
   submitWorkerInfo: function(e) {
      var param = e.detail.value //获取form表单内容
      console.log('e',e)
      param.Biz_ID = _opt.biz_ID
      param.User_ID = app.Data.userInfo.User_ID
      param.unionid = app.Data.userInfo.unionid
      param.Waiter_ID = _data.workerList[e.currentTarget.id].Waiter_ID
      console.log('param==',param)
      co.getShareApi('jyhs/Usermg/saveWorker', param, function(res) {
         if (res.code == 0) {
            _this.setData({
               edit_id: 0
            })
            _this.onLoad()
         }
      })
   },

   //编辑员工
   editWorker: function(e) {
      if (_opt.User_Identity == 'boss' || _opt.User_Identity == 'worker') {
         var index = e.currentTarget.id
         this.setData({
            edit_id: _data.edit_id == index ? -1 : index
         })
      } else {
         co.toastAllText('需要掌柜登录才能操作')
      }
   },

   //删除服务员
   delConfirm: function(e) {
      if (_opt.User_Identity == 'boss' || _opt.User_Identity == 'worker') {
         co.confirm('确认删除吗？', '取消', '删除', function(res) {
            if (res) {
               _this.delWaiter(e)
            }
         })
      } else {
         co.toastAllText('需要掌柜登录才能操作')
      }
   },

   delWaiter: function(e) {
      if (_opt.User_Identity == 'boss') {
         let i = e.currentTarget.id
         var list = _data.workerList
         co.getShareApi('jyhs/Usermg/deleteWorker', {
            Waiter_ID: list[i].Waiter_ID
         }, function(res) {
            if (res.code == 0) {
               list.splice(i, 1)
               _this.setData({
                  workerList: list
               })
            }
         })
      } else {
         co.toastAllText('需要掌柜登录才能操作！')
      }
   },

   //读取显示小程序码图片
   createQRCode() {
      var param = app.Data.app
      param.scene = "BizID." + _opt.biz_ID + '.InviterID.' + app.Data.userInfo.User_ID
      // param.scene = "BizID." + _opt.biz_ID + '.InviterID.' + 4377
      param.wxapp_page_path = '04myCenter/07workerEnterprise/worker/agreeTo/agreeTo'
      var url = "https://h5.jyhs.ls293.cn/" + param.appid + '/' + param.wxapp_page_path + "/" + param.scene + 's.png'
      _this.setData({
         inviteQRCode: url
      })
      _qrParam = param
   },

   //小程序二维码图片加载错误时回调
   onImgError: function() {
      co.getShareApi('api/Weixin/getWxAppCodeUnlimit', _qrParam, function(res) {
         if (res.code == 0) {
            _this.setData({
               inviteQRCode: res.data.wxapp_code_url
            })
         }
      })
   },

   //判断图片是否存在
   getImageInfo: function(img, callback) {
      wx.getImageInfo({
         src: img,
         success: function(res) {
            console.log('【wx.getImageInfo success:res】', res)
            callback(1)
         },
         fail: function(res) {
            console.log('【wx.getImageInfo fail:res】', res)
            callback(0)
         },
      })
   },

   //分享
   onShareAppMessage: function() {
      var urlParam = ''
      if (app.Data.userInfo.User_ID) {
         urlParam = '?scene=BizID.' + _opt.biz_ID + '.InviterID.' + app.Data.userInfo.User_ID
         // urlParam = '?scene=BizID.' + _opt.biz_ID + '.InviterID.' + 4377
      }
      return {
         title: '',
         path: '/04myCenter/07workerEnterprise/worker/agreeTo/agreeTo' + urlParam,
         success: function(res) {
            console.log("【分享成功res】===", res)
            // var pages = getCurrentPages() //获取加载的页面
            // var currentPage = pages[pages.length - 1] //获取当前页面的对象
            // app.getShareInfo(res, '邀请服务员', currentPage.route)
         }
      }
   },

})